<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="员工管理">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style>
        .openTable-item-value{
            font-size: 12px;
        }
    </style>

    <script>
        let admin=null;


        fixTableOperatorButton();
        jQuery(function () {
            get("${baseUrl}organization/store/getStores.action",function (resp) {
                if (resp.code == 1) {
                    var $select = $("[name='searchStoreId']");
                    $select.empty();
                    $select.append("<option value=''>全部</option>");
                    $.each(resp.data,function (i,data) {
                        $select.append("<option value='"+data.id+"'>"+data.name+"</option>");
                    });
                    formRender();
                }
            });
        });


        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;
            get("${baseUrl}user/getAdmin",function (res) {
                if (res.code == 1){
                    admin = res.data;
                }
                renderTable();
            });

            table.on('tool(table)', function (obj) {

                var data = obj.data;
                if (admin == null) {
                    return;
                }

                if(data.id == admin.id){
                    data['admin'] = 1;
                } else {
                    data['admin'] = 0;
                }

                switch(obj.event){
                    case 'password':
                        openTab("用户<"+data.name+">修改密码","${baseUrl}user/userChangePassword.action?userId="+data.id);
                        break;
                    case 'delete':
                        del(data);
                        break;
                    case 'edit':
                        openTab("用户<" + data.name+">编辑","${baseUrl}user/userEdit.action?userId="+data.id);
                        break;
                };
            });

            //监听事件
            table.on('toolbar(table)', function(obj){
                switch(obj.event){
                    case 'add':
                        openTab("新增员工","${baseUrl}user/userNew.action");
                        break;
                };
            });



            form.on('submit(search)', function (data) {
                var json = data.field;
                table.reload('userTable',{
                    where: json
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
                return false;
            });
        });

        function renderTable() {
            layui.use(['openTable'], function () {
                var openTable = layui.openTable;
                openTable.render({
                    id:'table'
                    , elem: '#table'
                    ,toolbar: '#tableToolBar'
                    ,defaultToolbar:[]
                    , url: "${baseUrl}user/user/getUsers.action?time="+(new Date()).valueOf()
                    , limit: 10
                    , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                        layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                        //,curr: 5 //设定初始在第 5 页
                        ,groups: 5 //只显示 5 个连续页码
                        ,first: false //不显示首页
                        ,last: false //不显示尾页
                    }
                    , done: function(res, curr, count){
                        setTableLineCss("table",res.data, "status", 2, "font-gray-c2c2c2");
                    }
                    , cols: [[
                        {width:70,title:"序号",templet:function (d) {
                                return genTableIndex(d.status==2, "锁定", d);
                            }},
                        {field: 'username', width: 150, title: '用户名',style:"font-size:12px;"},
                        {field: 'name', width: 150, title: '姓名',style:"font-size:12px;"},
                        {width: 150, title: '角色',style:"font-size:12px;",templet: function(d){
                                var roles = "";
                                $.each(d.roleList, function(i,role){
                                    roles += role.name + "、";
                                });
                                roles = roles.substring(0, roles.length-1);
                                return roles;
                            }},
                        {field: 'sex', width: 60, title: '性别',style:"font-size:12px;",templet: function(d){
                                return d.sex == 1? "男" : "女";
                            }},

                        {width: 80, title: '门店',style:"font-size:12px;",templet: function(d){
                                return d.store.name;}},
                        {field: 'phone', width: 110, title: '手机',style:"font-size:12px;"},
                        {field: 'email', width: 130, title: '邮箱',style:"font-size:12px;"},
                        {field: 'address', minWidth: 200, title: '地址', style:"font-size:12px;"},
                        {title: '操作', minWidth: 80, fixed:'right', templet: '#operBar'},
                    ]]

                    //注：展开的列Items
                    , openCols: [
                        {field: 'username',title: '用户名'},
                        {field: 'name', title: '姓名'},
                        {title: '角色',templet: function(d){
                            var roles = "";
                            $.each(d.roleList, function(i,role){
                                roles += role.name + "、";
                            });
                            roles = roles.substring(0, roles.length-1);
                            return genOpenTableContent("角色",roles);
                        }},
                        {field: 'sex', title: '性别',templet: function(d){
                            let sex = d.sex == 1? "男" : "女";
                            return genOpenTableContent("性别",sex);
                        }},
                        {field: 'status', title: '状态',templet: function(d){
                            let status = d.status == 1? "正常" : "锁定";
                            return genOpenTableContent("状态",status);
                        }},

                        {title: '所属门店',templet: function(d){
                            return genOpenTableContent("所属门店",d.store.name);
                        }},
                        {field: 'phone', title: '手机'},
                        {field: 'email', title: '邮箱'},
                        {field: 'address', title: '地址'},
                        // {title: '价格权限',templet: function(d){
                        //     var price="";
                        //     $.each(d.price.split("|"), function(i,code){
                        //         if (code == "1"){
                        //             price += "零售价、";
                        //         } else if(code == "2") {
                        //             price += "批发价、";
                        //         } else if(code == "3") {
                        //             price += "参考进货价、";
                        //         } else if(code == "4") {
                        //             price += "最低售价、";
                        //         } else if(code == "5") {
                        //             price += "成本价、";
                        //         } else if(code == "6") {
                        //             price += "等级价、";
                        //         }
                        //     });
                        //     price = price.substring(0, price.length-1);
                        //     return genOpenTableContent("价格权限",price);
                        // }},
                        // {title: '关联仓库',templet: function(d){
                        //     if(admin.id == d.id){
                        //         return genOpenTableContent("关联仓库","所有仓库");
                        //     }
                        //
                        //     var linkHouse = "";
                        //     $.each(d.houseList, function(i,house){
                        //         linkHouse += house.name + "、";
                        //     });
                        //     linkHouse = linkHouse.substring(0, linkHouse.length-1);
                        //     return genOpenTableContent("关联仓库",linkHouse);
                        // }},
                        {title: '查看他人单据',templet: function(d){
                            return genOpenTableContent("查看他人单据",d.viewOtherOrder==1 ? "允许":"不允许");
                        }},
                        {title: '作废他人单据',templet: function(d){
                            return genOpenTableContent("作废他人单据",d.cancelOtherOrder==1 ? "允许":"不允许");
                        }},
                        // {title: '查看他人客户',templet: function(d){
                        //     return genOpenTableContent("查看他人客户",d.viewOtherCustom==1 ? "允许":"不允许");
                        // }},
                        // {title: '查看其他门店库存',templet: function(d){
                        //     return genOpenTableContent("查看其他门店库存",d.viewOtherHouse==1 ? "允许":"不允许");
                        // }},
                        // {title: '从其他门店调拨',templet: function(d){
                        //     return genOpenTableContent("从其他门店调拨",d.transferOtherHouse==1 ? "允许":"不允许");
                        // }},
                    ]
                });
            });


        }

        function del(data) {
            if (data.admin == 1){
                errTip("管理员不能删除");
                return;
            }
            layer.confirm('删除员工《'+data.name+'》?', {icon: 3, title:'删除员工'}, function(index){
                tipLoad("删除中,请稍后...");
                post("${baseUrl}user/user/delete.action",{id:data.id}, function (resp) {
                    closeAll();
                    if (resp.code == 1){
                        renderTable();
                    } else {
                        errTip(resp.msg);
                    }
                });
            });
        }
    </script>

</html:header>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">门店</label>
                    <div class="layui-input-inline">
                        <select name="searchStoreId">
                        </select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">其他信息</label>
                    <div class="layui-input-inline">
                        <input type="text" name="searchContent" id="searchContent" placeholder="用户名、姓名或手机号" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <a class="layui-btn" lay-submit="" lay-filter="search">
                        <i class="layui-icon layui-icon-search"></i>
                    </a>
                </div>
            </div>
        </form>

        <div class="horizon-line"></div>

        <table class="layui-hide" id="table" lay-filter="table" lay-size="sm"></table>

        <script type="text/html" id="tableToolBar">
            <div class="layui-inline">
                <a class="layui-btn" lay-event="add">新增</a>
            </div>
        </script>

        <script type="text/html" id="operBar">
            <a class="layui-btn layui-btn-xs layui-btn-normal table-operator" lay-event="password">改密</a>
            {{# if(admin.id != d.id){ }}
            <a class="layui-btn layui-btn-xs table-operator" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger table-operator" lay-event="delete">删除</a>
            {{# } }}
        </script>
    </div>
</div>

</body>
<html:loading/>
</html>